package com.shigen.demo.atguigu;

/**
 * 集合不安全以及解决方案
 *
 * @author shigenfu
 * @date 2022/5/21 10:52 下午
 */
public class CollectionNotsafeDemo {

  public static void main(String[] args) {
    // List<String> stringList = Arrays.asList("aa", "bb", "cc");
    // stringList.forEach(System.out::println);
    /**
     * arrayList 线程不安全的解决方案
     */
    // 1.List<String> stringList = new Vector<>();
    // 2.List<String> stringList = Collections.synchronizedList(new ArrayList<>());
    // 3.List<String> stringList = new CopyOnWriteArrayList<>();

    // for (int i = 0; i < 30; i++) {
    //   new Thread(() -> {
    //     stringList.add(UUID.randomUUID().toString().substring(0, 2));
    //     System.out.println(stringList);
    //   }, String.valueOf(i)).start();
    // }

  //   HashSet<String> stringHashSet = new HashSet<>();
  //   // 1.Set<Object> stringHashSet = Collections.synchronizedSet(new HashSet<>());
  //   // 2.CopyOnWriteArraySet<Object> stringHashSet = new CopyOnWriteArraySet<>();
  //
  //   for (int i = 0; i < 30; i++) {
  //     new Thread(() -> {
  //       stringHashSet.add(UUID.randomUUID().toString().substring(0,2));
  //       System.out.println(stringHashSet);
  //     }, String.valueOf(i)).start();
  //
  //   }

    // HashMap<String, String> stringStringHashMap = new HashMap<>();
    // 1.Map<Object, Object> stringStringHashMap = Collections.synchronizedMap(new HashMap<>());
    // ConcurrentHashMap<Object, Object> stringStringHashMap = new ConcurrentHashMap<>();
    //
    // for (int i = 0; i < 30; i++) {
    //   new Thread(() -> {
    //     stringStringHashMap.put(Thread.currentThread().getName(), UUID.randomUUID().toString().substring(0,2));
    //     System.out.println(stringStringHashMap);
    //   }, String.valueOf(i)).start();
    // }



  }




}
